
var tour;
var adultcount = 1;
var childcount = 0;
var adultprice;
var childprice;
var totalprice;

var payType;

apiready = function() {
    
   	tour = api.pageParam.tour;
   	
  
   	
   	adultprice = tour.price;
   	childprice =Number(tour.smallprice);
   	totalprice = adultprice;
   	

	$api.text($api.byId('type'), tour.type);
	$api.text($api.byId('name'), tour.name);
	$api.text($api.byId('adultprice'), adultprice.toFixed(2));
	$api.text($api.byId('childprice'), childprice.toFixed(2));
	$api.text($api.byId('totalprice'), totalprice.toFixed(2));
}


//提交订单
function buy(){
	
	payType =$api.val($api.dom('input[name="payType"]:checked'));
	var linkman = $api.val($api.byId('linkman'));
	var mobile = $api.val($api.byId('mobile'));

	if(linkman==''){

		toast('请输入联系人!');
		return;
	}
	
	if(mobile==''){

		toast('请输入手机号!');
		return;
	}
	
	if(mobile.length!==11){

		toast('请正确输入手机号!');
		return;
	}

   // var data='account='+$api.getStorage('account')+'&name='+linkman+'&mobile='+mobile;
   var data={
       'account':$api.getStorage('account'),
       'proDefImg':tour.proDefImg,
       'merName':tour.merchantName,
       'name':linkman,
       'mobile':mobile,
       'tourname':tour.name,
       'tourId':tour._id,
       'godate':tour.godate,
       'day':tour.day,
       'peoplenum':adultcount+childcount,
       'buyNum':adultcount+childcount,
       'crnum':adultcount,
       'etnum':childcount,
       'crtotal':adultcount*tour.price,
       'ertatol':childcount*tour.smallprice,
       'proTotal':adultcount*tour.price+childcount*tour.smallprice,
       'total':adultcount*tour.price+childcount*tour.smallprice,
       'rTotal':adultcount*tour.price+childcount*tour.smallprice,
       'payType':payType,
      
       
   };
   
  // alert(JSON.stringify(data));
  // return;
   do_ajax('/app/tourorder/add','post',data,function(respose,err){
        api.hideProgress();
		if (respose.resultCode == "0") {
				var orderinfo= {
					orderNo: respose.orderNo,
					total:adultcount*tour.price+childcount*tour.smallprice,
					payType:payType,
				};
				
			   api.openWin({
			        name: 'pay',
			        url: "../pay.html",
			        pageParam: {orderinfo:orderinfo},
			       }
			   );
			   
	   }else{
	      alert(respose.datas);
	   }
	   
	});
	

	
	
	
};



function reduce1(){
	adultcount = Number($api.byId('adultcount').value);
	
	if(adultcount > 1){
		adultcount -= 1;
		update();
	}
}


function add1(){
	adultcount = Number($api.byId('adultcount').value);
	
	adultcount += 1;
	update();
}


function reduce2(){
	childcount = Number($api.byId('childcount').value);
	
	if(childcount > 0){
		childcount -= 1;
		update();
	}
}


function add2(){
	childcount = Number($api.byId('childcount').value);
	
	childcount += 1;
	update();
}


function update(){
	$api.val($api.byId('adultcount'), adultcount);
	$api.val($api.byId('childcount'), childcount);
	
	totalprice = adultprice * adultcount + childprice * childcount;
	totalprice = totalprice.toFixed(2);
	
	$api.text($api.byId('totalprice'), totalprice);
}